package com.hnevc.mukewangapp;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class NotesFragment extends Fragment {


    List<Fragment> fragmentList;
    ViewPager mViewPager;
    RadioGroup radioGroup;


    public NotesFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_notes, container, false);
        mViewPager = view.findViewById(R.id.viewpager1);
        radioGroup = view.findViewById(R.id.yan1);
        return  view;
    }








    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        fragmentList = new ArrayList<>();
        fragmentList.add(new notes_one_Fragment());
        fragmentList.add(new notes_two_Fragment());

        mViewPager.setAdapter(new MyFragmentAdapter(getChildFragmentManager()));

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                switch (position){
                    case 0:
                        radioGroup.check(R.id.yan1_wen);
                        break;
                    case 1:
                        radioGroup.check(R.id.yan1_wen2);
                        break;
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.yan1_wen:
                        if(mViewPager.getCurrentItem()==1) {
                            mViewPager.setCurrentItem(0,false);
                        }
                        break;
                    case R.id.yan1_wen2:
                        if(mViewPager.getCurrentItem() == 0) {
                            mViewPager.setCurrentItem(1,false);
                        }
                        break;
                }
            }
        });
    }
    class  MyFragmentAdapter extends FragmentPagerAdapter {

        public MyFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }



}
